import hashlib
import blogger

STATIC_MAPS_URL = "http://maps.googleapis.com/maps/api/staticmap?"
DEFAULT_QUALITY = 75

class PhotoPlacement(object):
    def __init__(self, index, x, y, rotation, size):
        self.index = max(int(index), 0)
        self.x = int(x)
        self.y = int(y)
        self.rotation = int(rotation)
        self.size = size
        
        
    @staticmethod
    def from_querystring(text):
        values = [0, 0, 0, 0, 'Medium']
        for n, value in enumerate(text.split('|')):
            values[n] = value
        return PhotoPlacement(*values)

class Query(object):
    def __init__(self, request):
        self.request = request
        
    def hash(self):
        return hashlib.md5(self.request.query_string).hexdigest()
    
    def get_map_parameters(self):
        parameters = {}
        for key in self.MAP_PARAMETERS:
            value = self.request.get(key)
            if value:
                parameters[key] = value
        return parameters
    
    def get_photo_placements(self):
        photos = []
        for parameters in self.request.get_all('photo'):
            photos.append(PhotoPlacement.from_querystring(parameters))
        return photos
    
    def __get_scalar(self, key, default=1.0):
        value = self.request.get(key, default)
        try:
            value = float(value)
            value = max(value, 0.0)
            value = min(value, 1.0)
        except:
            value = 1.0
        return value
    
    def __get_boolean(self, key, default):
        value = self.request.get(key, str(default))
        value = value.lower() == 'true'
        return value
    
    @property
    def color(self):
        return self.__get_scalar('color')
    
    @property
    def brightness(self):
        return self.__get_scalar('brightness')
    
    @property
    def blur(self):
        return self.__get_boolean('blur', False)
    
    @property
    def quality(self):
        value = self.request.get('quality', DEFAULT_QUALITY)
        try:
            value = int(value)
            value = max(value, 0)
            value = min(value, 100)
        except:
            value = DEFAULT_QUALITY
        return value
    
    @property
    def length(self):
        value = self.request.get('length', blogger.DEFAULT_MAX_ROUTE_LENGTH)
        try:
            value = int(value)
            value = max(value, 0)
        except:
            value = blogger.DEFAULT_MAX_ROUTE_LENGTH
        return value
    
    @property
    def offset(self):
        offset = self.request.get('offset')
        if not offset:
            return 0.0, 0.0
        try:
            latitude, longitude = offset.split(',')
            latitude = float(latitude)
            longitude = float(longitude)
            return latitude, longitude
        except:
            return 0.0, 0.0
        
    MAP_PARAMETERS = ('zoom', 'maptype', 'language', 'region', 'visible', 'style', 'size')